import 'package:dio/dio.dart';
import 'package:get/get.dart';

import '../../../bean/bean_train.dart';
import '../../../net/dio_utils.dart';
import '../../../utils/toast_utils.dart';

class TrainDegreeOfStatisfactionItemController extends GetxController {
  int trainId;

  TrainDegreeOfStatisfactionItemController(this.trainId);

  TrainUserCheckRecoderEntity _checkRecoderEntity;
  bool _isLoading = true;

  bool getIsLoading() {
    return _isLoading;
  }

  TrainUserCheckRecoderEntity getTrainUserCheckRecoderEntity() {
    return _checkRecoderEntity;
  }

  CancelToken _signCancelToken;

  Future<bool> commitTrainDegreeOfStatisfactionRequest(
      {bool click = false}) async {
    _isLoading = true;
    if (click) {
      update(["root"]);
    }

    if (_signCancelToken != null && !_signCancelToken.isCancelled) {
      _signCancelToken.cancel();
    }
    _signCancelToken = new CancelToken();
    List detreeDetails = [];
    for (int i = 0; i < list.length; i++) {
      Map<String, dynamic> map = list[i];
      List<Map<String, dynamic>> values = map["values"];
      for (int k = 0; k < values.length; k++) {
        Map<String, dynamic> map = values[k];
        detreeDetails.add(map);
      }
    }
    //答题数据
    ResponseInfo statisRresponseInfo = await DioUtils.instance.postRequest(
        url: HttpHelper.trainDegreeOfStatisFacionAdd,
        cancelTag: _signCancelToken,
        jsonMap: {
          "trainId": trainId,
          "detreeDetails": detreeDetails,
          "attendanceNumber": getTotalScore(),
        });
    if (statisRresponseInfo.success) {
      Map data = statisRresponseInfo.data;
      _checkRecoderEntity = TrainUserCheckRecoderEntity.fromMap(data);
    } else {
      ToastUtils.showToast("操作失败 " + statisRresponseInfo.message);
      return false;
    }
    ToastUtils.showToast("操作成功");
    Get.back(result: true);
    _isLoading = false;
    update(["root"]);
    return true;
  }

  List<Map<String, dynamic>> list = [
    {
      "key": "一、培训课程内容满意度",
      "values": [
        {
          "key": "1、您认为培训内容的实用性",
          "values": [1, 2, 3, 4, 5],
          "index": 1,
          "selectValue": "",
        },
        {
          "key": "2、您对培训内容的记忆程度",
          "index": 2,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "3、您认为培训内容满足您需求的程度",
          "index": 3,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "4、您认为培训课程案例的丰富程度",
          "index": 4,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "5、您认为培训内容的前后连贯性",
          "index": 5,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "6、您认为培训课程整体架构的合理性",
          "index": 6,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "7、您认为对您个人知识和技能的提升",
          "index": 7,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "8、您认为对您在实际工作中的指导性",
          "index": 8,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "9、您认为在课程时间安排的合理性",
          "index": 9,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
      ],
    },
    {
      "key": "二、培训教师能力满意度",
      "values": [
        {
          "key": "1、您认为培训教师的仪态举止",
          "values": [1, 2, 3, 4, 5],
          "index": 1,
          "selectValue": "",
        },
        {
          "key": "2、您认为培训教师的讲课技巧",
          "index": 2,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "3、您认为培训教师的语言清晰程度",
          "index": 3,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "4、您认为培训教师在培训主题把握方面",
          "index": 4,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "5、您认为培训教师讲授内容的深浅度",
          "index": 5,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "6、您认为培训教师课堂上和学员的互动性",
          "index": 6,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "7、您认为培训教师个人的专业和技能",
          "index": 7,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "8、您认为培训教师课堂气氛的控制",
          "index": 8,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
      ],
    },
    {
      "key": "三、培训教学管理和现场服务满意度",
      "values": [
        {
          "key": "1、您认为培训通知发布的及时准确度",
          "values": [1, 2, 3, 4, 5],
          "index": 1,
          "selectValue": "",
        },
        {
          "key": "2、您认为培训教学用品准备齐全程度",
          "index": 2,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "3、您认为培训课堂及日常纪律管理",
          "index": 3,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "4、您认为培训班班主任的服务",
          "index": 4,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "5、您认为培训教室的布置",
          "index": 5,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "6、您认为培训教学设备",
          "index": 6,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
      ],
    },
    {
      "key": "四、培训后勤服务和质量满意度",
      "values": [
        {
          "key": "1、您认为培训提供的餐饮质量满意度",
          "values": [1, 2, 3, 4, 5],
          "index": 1,
          "selectValue": "",
        },
        {
          "key": "2、您认为培训提供餐饮菜品数量满意度",
          "index": 2,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "3、您认为培训提供餐饮服务满意度",
          "index": 3,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "4、您认为培训提供住宿安排满意度",
          "index": 4,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "5、您认为培训住宿服务质量和态度",
          "index": 5,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
      ],
    }
  ];

  int getTotalScore() {
    int total = 0;

    for (int i = 0; i < list.length; i++) {
      Map<String, dynamic> map = list[i];
      List<Map<String, dynamic>> values = map["values"];
      for (int k = 0; k < values.length; k++) {
        Map<String, dynamic> map = values[k];
        String selectValue = map["selectValue"];
        if (selectValue.isNotEmpty) {
          total += int.parse(selectValue);
        }
      }
    }
    return total;
  }

  int getNoTotalNumber() {
    int total = 0;

    for (int i = 0; i < list.length; i++) {
      Map<String, dynamic> map = list[i];
      List<Map<String, dynamic>> values = map["values"];
      for (int k = 0; k < values.length; k++) {
        Map<String, dynamic> map = values[k];
        String selectValue = map["selectValue"];
        if (selectValue.isEmpty) {
          total++;
        }
      }
    }
    return total;
  }
}
